package vip.zhaotao.controller;

import com.google.common.collect.Lists;
import freemarker.template.TemplateException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import vip.zhaotao.base.BaseController;
import vip.zhaotao.utils.FileUtils;
import vip.zhaotao.utils.WordUtils;
import vip.zhaotao.utils.ZipUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Word Controller
 */
@Controller
public class WordController extends BaseController {

    /**
     * 生成zip文件（文件内容包含多个word文档）
     *
     * @param response
     */
    @RequestMapping(value = "/zip", method = RequestMethod.GET)
    public void genZip(HttpServletResponse response) throws IOException, TemplateException {
        String responseFileName = StringUtils.join("test_", System.currentTimeMillis(), ".zip");
        this.setZipHeader(response, responseFileName, FileUtils.utf8().name());

        File tempDirectory = FileUtils.generateTempDirectory();
        List<String> list = Lists.newArrayList("230227198302151067", "371522199402189127");
        for (String idCard : list) {
            Map<String, Object> dataModel = new HashMap<>();
            dataModel.put("content", System.currentTimeMillis());
            WordUtils.generateDocxFile(
                    new FileOutputStream(new File(tempDirectory, StringUtils.join(idCard, ".docx"))),
                    dataModel);
        }
        ZipUtils.generateZipFileByDirectory(tempDirectory, response.getOutputStream());
        FileUtils.deleteTempDirectory(tempDirectory);
    }

    /**
     * 生成docx文件
     *
     * @param response
     */
    @RequestMapping(value = "/docx", method = RequestMethod.GET)
    public void genDocx(HttpServletResponse response) throws IOException, TemplateException {
        // 响应文件名称（即最终下载的文件名称）
        String responseFileName = "test.docx";
        this.setDocxHeader(response, responseFileName, FileUtils.utf8().name());

        Map<String, Object> dataModel = new HashMap<>();
        dataModel.put("content", System.currentTimeMillis());
        WordUtils.generateDocxFile(response.getOutputStream(), dataModel);
    }

    /**
     * 生成doc文件
     *
     * @param response
     * @throws IOException
     * @throws TemplateException
     */
    @RequestMapping(value = "/doc", method = RequestMethod.GET)
    public void genDoc(HttpServletResponse response) throws IOException, TemplateException {

        // 响应文件名称（即最终下载的文件名称）
        String responseFileName = "test.doc";
        this.setDocHeader(response, responseFileName, FileUtils.utf8().name());

        Map<String, Object> dataModel = new HashMap<>();
        dataModel.put("content", System.currentTimeMillis());
        WordUtils.generateDocFile(response.getOutputStream(), dataModel);
    }
}
